/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.agency;

import java.awt.Window;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.LoggedData.HistoricallyLoggedData;
import researchgrants.parts.db.Db;

/**
 * A single Agency
 * @author DOStudent1
 */
public class Agency {
    // Displays AgencyView to input a new agency
    static Agency inputNew(Window parent) {
        // initializes an unbound Agency Contact View
        AgencyView viewNew = new AgencyView(parent);
        Agency newAgency=viewNew.getAgency();
        return (newAgency);
    }

    // retrieve an existing Agency
    static Agency getById(int id) {
        return(new Agency(id));
    }

    // the identification number of the Agency
    private int id;
    
    // the fields to contain the elements of the Agency
    private HistoricallyLoggedData agencyName;
    private HistoricallyLoggedData agencyCategoryID;
    private HistoricallyLoggedData notes;
    
    // a private constructor
    private Agency(int id) {
        this.id=id;
        syncDataFromDb();
    }

    // populate the fields from the database
    private void syncDataFromDb() {
        try {
            Connection conn = Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT AgencyNameRef, AgencyCategoryIDRef, NotesRef FROM tblAgencies WHERE AgencyID=?");
            prepareStatement.setInt(1, id);
            ResultSet agencyResultSet = prepareStatement.executeQuery();
            Db.moveToFirstLine(agencyResultSet);

            agencyName=HistoricallyLoggedData.getById((Integer) Db.getColumn(agencyResultSet, 1));
            agencyCategoryID=HistoricallyLoggedData.getById((Integer) Db.getColumn(agencyResultSet, 2));
            notes=HistoricallyLoggedData.getById((Integer) Db.getColumn(agencyResultSet, 3));
            
            agencyResultSet.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(Agency.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Agency.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /////////////////////////////////////////
    // Retreive the different elements' values
    /////////////////////////////////////////
    
    public int getId() {
        return(id);
    }
    
    public AgencyCategory getCurrentAgencyCategory() {
        return(AgencyCategory.getByID(Integer.parseInt(agencyCategoryID.value())));
    }

    
    public String getCurrentAgencyName() {
        return(agencyName.value());
    }
    
    public String getCurrentAgencyNotes() {
        return(notes.value());
    }
    
    /////////////////////////////////////////////////////////////////
    // Retreive the raw data - to be used to input a new agency
    /////////////////////////////////////////////////////////////////
    
    HistoricallyLoggedData getAngecyName() {
        return(agencyName);
    }
    
    HistoricallyLoggedData getAgencyNotes() {
        return(notes);
    }
    
    HistoricallyLoggedData getAgencyCategoryId() {
        return(agencyCategoryID);
    }

    
    // Shows this Agency
    void show(Window parent) {
        new AgencyView(parent, this);
    }

}
